/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketmaster.businesslogic;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import ticketmaster.businessobject.SeatBO;
import ticketmaster.dao.BookedTicketDAO;
import ticketmaster.dao.DAOFactory;
import ticketmaster.dao.SeatDAO;
import ticketmaster.dataobject.BookedTicket;
import ticketmaster.dataobject.Seat;
import ticketmaster.dataobject.TicketRate;
import ticketmaster.util.DateTimeHelper;

/**
 *
 * @author Wei Liu
 */
public class SeatController
{

    private static final int EXPIRE_TIME = -10;

    public ArrayList<SeatBO> getAllAvailableSeats(TicketRate ticketRate, int numberOfSeats, int eventID)
    {
        BookedTicket bookedTicket = new BookedTicket();
        //bookedTicket.setEventID(ticketRate.getEventID());
        //bookedTicket.setVenueID(ticketRate.getVenueID());
        //bookedTicket.setSeatCategoryID(ticketRate.getSeatCategoryID());
        //bookedTicket.setIsPaid(true);
        //bookedTicket.setIsCancelled(false);
        Calendar c = DateTimeHelper.getCurrentDateTimeCalendar();
        c.add(Calendar.MINUTE, EXPIRE_TIME);
        Timestamp ts = new Timestamp(c.getTimeInMillis());
        bookedTicket.setSeatCategoryID(ticketRate.getSeatCategoryID());
        bookedTicket.setBookedDate(ts);

        BookedTicketDAO bookedTicketDAO = DAOFactory.getInstance().getBookedTicketDAO();
        bookedTicketDAO.updateBookedTicketStatus(bookedTicket);
        Collection<BookedTicket> bookedTickets = bookedTicketDAO.getAvailableBookedTicketsDO(bookedTicket);

        Seat criteria = new Seat();
        criteria.setSeatCategoryID(ticketRate.getSeatCategoryID());
        criteria.setVenueID(ticketRate.getVenueID());

        Seat seat = DAOFactory.getInstance().getSeatDAO().findSeat(criteria);

        //Collection<BookedTicket> unavailableTickets = this.getUnavailableTickets(bookedTickets);

        // not enough seat available
//        if (seat.getNumberOfSeats() - unavailableTickets.size() < numberOfSeats)
//        {
//            return null;
//        }
        if (seat.getNumberOfSeats() - bookedTickets.size() < numberOfSeats)
        {
            return null;
        }

        int seatNumber = 0;
        int bookNumber = numberOfSeats;
        ArrayList<SeatBO> seatBOs = new ArrayList<SeatBO>();


        while (seatNumber < seat.getNumberOfSeats())
        {
            boolean deleted = false;

            for (BookedTicket ticket : bookedTickets)
            {
                if (seatNumber == ticket.getSeatNumber() && eventID == ticket.getEventID())
                {
                    bookedTickets.remove(ticket);
                    seatNumber++;
                    deleted = true;
                    break;
                }
            }

            if(deleted)
            {
                continue;
            }

            SeatBO seatBO = new SeatBO();
            seatBO.setSeatNumber(seatNumber);
            seatBO.setPrice(ticketRate.getPrice());
            if (bookNumber > 0)
            {
                seatBO.setSelected(true);
                bookNumber--;
            }
            else
            {
                seatBO.setSelected(false);
            }
            seatBOs.add(seatBO);
            seatNumber++;
        }

        return seatBOs;
    }

    private Collection<BookedTicket> getUnavailableTickets(Collection<BookedTicket> tickets)
    {
        Calendar calendar = Calendar.getInstance();

        for (BookedTicket ticket : tickets)
        {
            Calendar cal = Calendar.getInstance();
            cal.setTime(ticket.getBookedDate());
            cal.add(Calendar.MINUTE, 10);
            if (cal.before(calendar) || ticket.isIsPaid())
            {
                tickets.remove(ticket);
            }

            if (tickets.size() == 0)
            {
                break;
            }
        }

        return tickets;
    }

    public Collection<Seat> getAllSeat(Seat seat)
    {
        SeatDAO seatDAO = DAOFactory.getInstance().getSeatDAO();
        return seatDAO.getSeatCollection(seat);
    }
}
